<?php
namespace Admin\Controller;
use Think\Controller;
/**
 * 后台管理员对用户的管理
 */


class UserController extends CommonController {

		public function index()
		{

			//用户列表的的分页
			 $count =  M('user')->where(array('is_admin'=>'0','shw'=>'1'))->count();
			 $this->assign('count',$count);
			$Page = new \Think\Page($count,5);
			$show = $Page->show();
			
			$this->assign('page',$show);
		
			$sql = "select * from user where is_admin='0' and shw='1' order by create_time desc limit ".$Page->firstRow.",".$Page->listRows;
			
			$user = M('user')->query($sql);
			// var_dump($user);
			$this->assign('users',$user);
			$this->display();
		}

		//后台用户的状态
		public function memberstop()
		{
				$uid = I('post.uid');
				$res =M('user')->where(array('id'=>$uid))->getField('stop');
	    	if($res==0){
	    		M('user')->where(array('id'=>$uid))->save(array('stop'=>1));
	    		$arr = array(	
	    			'error'=>0,
	    			'info'=>'已停用'
	    			);
	    		echo json_encode($arr);
	    		exit;

	    	}else{
	    		M('user')->where(array('id'=>$uid))->save(array('stop'=>0));
	    		$arr = array(
	    			'error'=>1,
	    			'info'=>'启用'
	    			);
	    		echo json_encode($arr);
	    		exit;
	    	}
		}
		// public function member_add()
		// {
		// 	$this->display();
		// }
		//添加用户
		public function checkdate()
		{
					//验证密码是否一致
			$password = I('post.password');
			
			$email = I('post.email');
			//验证邮箱
			if (D('user')->checkEmail($email)) {
						$arr = array(
						'error' => 1,
						'info' =>'邮箱已经存在重新输入'
					);
				echo json_encode($arr);
				exit();
			}

			if (D('user')->checkNickname($nickname)) {
						$arr = array(
						'error' => 1,
						'info' =>'昵称已存在重新输入'
					);
				echo json_encode($arr);
				exit();
			}

			$data = I('post.');
			$data['password'] = md5($password);
			$data['create_time'] = time();
			if (M('user')->add($data)) {
				$arr = array(
					'error' => 0,
					'info' => "添加成功"
					);
				echo json_encode($arr);
				exit();
			}else{
				$arr = array(
					'error' => 1,
					'info' => '添加失败'
					);
				echo json_encode($arr);
				exit();
			}

		}


				//基本设置展示
		public function member_edit()
		{
			$id = I('get.id');

			// var_dump($id);

			$res = D('user')->where(array('id'=>$id))->find();

			// var_dump($res);
			$this->assign('res',$res);
			// layout('left');
			$this->display();
			
			
		}



					//更新基本设置数据数据,用户里面的修改
			public function editdate()
			{
				$data = I('post.');
				
				$id = I('post.id');
				
				$sql = "select * from user where id!={$id} && (email='{$data['email']}' || nickname='{$data['nickname']}')";
				$res = M('user')->query($sql);
				
				if ($res) {
					$arr = array(
					'error'=>0,
					'info'=>'邮箱或者用户名已经存在'
				);
				echo json_encode($arr);
				}else{
						if ($data['sex']==0) {
						$data['sex'] = "男";
					}else{
						$data['sex'] = '女';
					}
					$data['id'] = $id;
					// var_dump($data);
					M('user')->save($data);
					$arr = array(
						'error'=>1,
						'info'=>'修改成功'
					);
					echo json_encode($arr);
				}
				


			}



			//管理员修改用户密码界面
			public function member_password()
			{
				$id = I('get.id');
				$user = M('user')->find($id);
				$this->assign('user',$user);
				$this->display();
			}
			//管理员修改用户密码
			public function changePass()
			{
				
				$id = I('post.id');
				// $data = I('post.');
				$password=I('post.newpass');
				$repassword = I('post.repass');
				$user = M('user')->where(array('id'=>$id))->find();
				// var_dump(I('post.'));
				// var_dump($user);
				if ($password!=$repassword&&md5(I('post.oldpass'))!=$user['password']) {
					$arr = array(
					'error'=>0,
					'info'=>'前后密码不一致或者或者原来密码错误'
				);
				echo json_encode($arr);
				exit();
				}
				$data['password'] = md5($password);
				// var_dump($data);

					M('user')->save($data);
					$arr = array(
					'error'=>1,
					'info'=>'修改成功'
					);
				echo json_encode($arr);
				
			}

						//批量删除用户(定个状态)
	   	public function delAll()
	   	{
	   		$id = current(I('post.'));
	   		
	   		$id = rtrim($id,',');
	   		$arr = explode(',', $id);
	   		// var_dump($arr);exit();
	   		// $sql = "update question set show='1' where id in ({$id})";
	   		foreach ($arr as $id) {
	   			$sql = "update user set shw='0' where id='{$id}'";
				M()->execute($sql);
	   		}
	   		// var_dump($sql);
	   				   	$arr = array(
		   				'error'=>0,
		   				'info'=>"删除成功",
		   				// 'url'=>U('admin/question/index')
		   			);
		   		 echo json_encode($arr);
		   		 exit();
	   		
	   	}
	   	   

			//批量恢复用户(定个状态)
	   	public function recoverAll()
	   	{
	   		$id = current(I('post.'));
	   		
	   		$id = rtrim($id,',');
	   		$arr = explode(',', $id);
	   		// var_dump($arr);exit();
	   		// $sql = "update question set show='1' where id in ({$id})";
	   		foreach ($arr as $id) {
	   			$sql = "update user set shw='1' where id='{$id}'";
				M()->execute($sql);
	   		}
	   		// var_dump($sql);
	   				   	$arr = array(
		   				'error'=>0,
		   				'info'=>"恢复成功",
		   				// 'url'=>U('admin/question/index')
		   			);
		   		 echo json_encode($arr);
		   		 exit();
	   		
	   	}
	   	   	//删除一条用户(状态)
	   public function	delte()
	   {
	   	  $id=current(I('post.'));
	   	  // $sql = "delete from question where id={$id}";
	   	 $sql = "update user set shw='0' where id={$id}";

	   	  if ( M()->execute($sql)) {
		   	 
		   	  $arr = array(
		   				'error'=>0,
		   				'info'=>"删除成功"
		   			);
		   	  echo json_encode($arr);
	   	  }
	   	 
	   }

	   //删除用户的展示
	   		public function member_del()
		{

			//用户列表的的分页
			 $count =  M('user')->where(array('is_admin'=>'0','shw'=>'0'))->count();
			 $this->assign('count',$count);
			$Page = new \Think\Page($count,5);
			$show = $Page->show();
			
			$this->assign('page',$show);
		
			$sql = "select * from user where is_admin='0' and shw='0' order by create_time desc limit ".$Page->firstRow.",".$Page->listRows;
			
			$user = M('user')->query($sql);
			// var_dump($user);
			$this->assign('users',$user);
			$this->display();
		}

		  public function recover()
	   {
	   		 $id=current(I('post.'));
	   		 $sql = "update user set shw='1' where id={$id}";
			 if ( M()->execute($sql)) {
		   	 
		   	  $arr = array(
		   				'error'=>0,
		   				'info'=>"恢复成功"
		   			);
		   	  echo json_encode($arr);
	   	  }
	   }
	      	//彻底删除一条问题
	   public function	delteone()
	   {
	   	  $id=current(I('post.'));
	   	  $sql = "delete from user where id={$id}";
	   	 // $sql = "update question set shw='1' where id={$id}";

	   	  if ( M()->execute($sql)) {
		   	 
		   	  $arr = array(
		   				'error'=>0,
		   				'info'=>"删除成功"
		   			);
		   	  echo json_encode($arr);
	   	  }
	   	 
	   }


}


?>